import { Injectable } from '@nestjs/common';

@Injectable()
export class GatewayService {
  private readonly services = {
    'user-service': { port: 3001, host: 'localhost' },
    'miniprogram-service': { port: 3002, host: 'localhost' },
    'h5-app-service': { port: 3003, host: 'localhost' },
    'config-service': { port: 3004, host: 'localhost' },
  };

  getServices() {
    return {
      code: 200,
      message: '获取服务列表成功',
      data: this.services,
      timestamp: Date.now(),
    };
  }

  getRoutes() {
    const routes = [
      { path: '/api/user/*', service: 'user-service' },
      { path: '/api/miniprogram/*', service: 'miniprogram-service' },
      { path: '/api/h5/*', service: 'h5-app-service' },
      { path: '/api/config/*', service: 'config-service' },
    ];

    return {
      code: 200,
      message: '获取路由列表成功',
      data: routes,
      timestamp: Date.now(),
    };
  }

  async proxyRequest(service: string, path: string, body: any, query: any) {
    const serviceConfig = this.services[service];
    if (!serviceConfig) {
      throw new Error(`服务 ${service} 不存在`);
    }

    // 这里应该实现实际的代理逻辑
    // 目前返回模拟数据
    return {
      code: 200,
      message: '代理请求成功',
      data: {
        service,
        path,
        body,
        query,
        target: `http://${serviceConfig.host}:${serviceConfig.port}`,
      },
      timestamp: Date.now(),
    };
  }
}

